package com.love.usedcar.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.love.usedcar.domain.dto.CityNodeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.love.common.log.annotation.Log;
import com.love.common.log.enums.BusinessType;
import com.love.common.security.annotation.RequiresPermissions;
import com.love.usedcar.domain.CityDirectory;
import com.love.usedcar.service.ICityDirectoryService;
import com.love.common.core.web.controller.BaseController;
import com.love.common.core.web.domain.AjaxResult;
import com.love.common.core.utils.poi.ExcelUtil;
import com.love.common.core.web.page.TableDataInfo;

/**
 * 城市编码Controller
 * 
 * @author love
 * @date 2025-10-12
 */
@RestController
@RequestMapping("/directory")
public class CityDirectoryController extends BaseController
{
    @Autowired
    private ICityDirectoryService cityDirectoryService;

    /**
     * 查询城市编码列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CityDirectory cityDirectory)
    {
        startPage();
        List<CityDirectory> list = cityDirectoryService.selectCityDirectoryList(cityDirectory);
        return getDataTable(list);
    }

    /**
     * 导出城市编码列表
     */
    @Log(title = "城市编码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CityDirectory cityDirectory)
    {
        List<CityDirectory> list = cityDirectoryService.selectCityDirectoryList(cityDirectory);
        ExcelUtil<CityDirectory> util = new ExcelUtil<CityDirectory>(CityDirectory.class);
        util.exportExcel(response, list, "城市编码数据");
    }

    /**
     * 获取城市编码详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(cityDirectoryService.selectCityDirectoryById(id));
    }

    /**
     * 新增城市编码
     */
    @Log(title = "城市编码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CityDirectory cityDirectory)
    {
        return toAjax(cityDirectoryService.insertCityDirectory(cityDirectory));
    }

    /**
     * 修改城市编码
     */
    @Log(title = "城市编码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CityDirectory cityDirectory)
    {
        return toAjax(cityDirectoryService.updateCityDirectory(cityDirectory));
    }

    /**
     * 删除城市编码
     */
    @Log(title = "城市编码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(cityDirectoryService.deleteCityDirectoryByIds(ids));
    }

    /**
     * 查询省份列表，供 OpenFeign 调用
     */
    @GetMapping(value = "/getProvinces")
    public AjaxResult selectProvinces() {
        List<HashMap<String, String>> provinces = cityDirectoryService.selectProvinces();
        return AjaxResult.success(provinces);
    }

    /**
     * 查询城市列表
     */
    @GetMapping(value = "/getCitys/{provinceCode}")
    public AjaxResult getCitys(@PathVariable String provinceCode) {
        List<HashMap<String, String>> citys = cityDirectoryService.selectCitys(provinceCode);
        return AjaxResult.success(citys);
    }

    /**
     * 新增：获取城市级联数据（给前端级联选择器用）
     */
    @GetMapping("/cascader")
    public AjaxResult getCityCascaderData() {
        List<CityNodeDTO> data = cityDirectoryService.getCityCascaderData();
        return AjaxResult.success(data); // 若依的 AjaxResult 封装成功响应
    }
}
